package ui;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RubberBandShapeDrawingApp {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame frame = new JFrame("橡皮筋线画圆和正方形程序");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(800, 600);
                
                final RubberBandDrawingPanel drawingPanel = new RubberBandDrawingPanel();
                frame.add(drawingPanel, BorderLayout.CENTER);
                
                
                
                
                
                // 添加菜单栏
                JMenuBar menuBar = new JMenuBar();
                
                // 文件菜单
                JMenu fileMenu = new JMenu("文件");
                
                // 保存菜单项
                JMenuItem saveItem = new JMenuItem("保存");
                saveItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser fileChooser = new JFileChooser();
                        int returnVal = fileChooser.showSaveDialog(frame);
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            File file = fileChooser.getSelectedFile();
                            try {
                                drawingPanel.saveShapesToFile(file);
                                JOptionPane.showMessageDialog(frame, "图形保存成功！");
                            } catch (Exception ex) {
                                JOptionPane.showMessageDialog(frame, "保存失败: " + ex.getMessage(), 
                                    "错误", JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    }
                });
                
                // 打开菜单项
                JMenuItem openItem = new JMenuItem("打开");
                openItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser fileChooser = new JFileChooser();
                        int returnVal = fileChooser.showOpenDialog(frame);
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            File file = fileChooser.getSelectedFile();
                            try {
                                drawingPanel.loadShapesFromFile(file);
                                JOptionPane.showMessageDialog(frame, "图形加载成功！");
                            } catch (Exception ex) {
                            	ex.printStackTrace();
                                JOptionPane.showMessageDialog(frame, "加载失败: " + ex.getMessage(), 
                                    "错误", JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    }
                });
                
                fileMenu.add(saveItem);
                fileMenu.add(openItem);
                menuBar.add(fileMenu);
                frame.setJMenuBar(menuBar);
                
                
                // 添加工具条
                JToolBar toolBar = new JToolBar();
                JButton circleButton = new JButton("圆形");
                JButton squareButton = new JButton("正方形");
                
                circleButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        drawingPanel.setShapeMode(ShapeMode.CIRCLE);
                    }
                });
                
                squareButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        drawingPanel.setShapeMode(ShapeMode.SQUARE);
                    }
                });
                
                
                
                
                
                
                
                
                
                toolBar.add(circleButton);
                toolBar.add(squareButton);
                frame.add(toolBar, BorderLayout.NORTH);
                
                frame.setVisible(true);
            }
        });
    }
}
